/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */

package cn.webrx;

import java.util.Arrays;
import java.util.Random;

/**
 * <p>Project: jse2303 - ArrSort1
 * <p>Powered by webrx On 2023-07-12 11:00:51
 * <p>描述：<p>
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class ArrSort1 {
    public static void main(String[] args) {
        //冒泡排序
        Random rand = new Random();
        //声明数组，自动初始化
        int[] ns = new int[10];
        System.out.println(Arrays.toString(ns));

        //赋值1-100随机整数
        for (int i = 0; i < 10; i++) {
            ns[i] = rand.nextInt(1, 101);
        }
        System.out.println(Arrays.toString(ns));

        //排序
        //Arrays.sort(ns);//默认升序
        //System.out.println(Arrays.toString(ns));
        //冒泡排序
        for (int i = 0; i < ns.length - 1; i++) {
            for (int j = 0; j < ns.length - 1 - i; j++) {
                //> 升序  < 降序
                if (ns[j] < ns[j + 1]) {
                    int t = ns[j];
                    ns[j] = ns[j + 1];
                    ns[j + 1] = t;
                }
                //System.out.println(Arrays.toString(ns));
            }
        }
        System.out.println(Arrays.toString(ns));

        //洗牌，打乱顺序
        for (int i = 0; i < ns.length * 2; i++) {
            int a = rand.nextInt(ns.length);
            int b = rand.nextInt(ns.length);
            if (a == b) {
                i--;
                continue;
            }
            int t = ns[a];
            ns[a] = ns[b];
            ns[b] = t;
        }
        System.out.println(Arrays.toString(ns));
    }
}
